package com.dbappsecurity.bifrost.view;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import butterknife.BindView;
import butterknife.ButterKnife;
import com.dbapp.uilib.widget.titlebar.TitleBar;
import com.dbappsecurity.bifrost.BuildConfig;
import com.dbappsecurity.bifrost.R;
import com.dbappsecurity.bifrost.base.InjectBaseFragment;
import com.dbappsecurity.core.CoreBaseFragment;
import com.dbappsecurity.jsbridge.JsBaseFragment;
import com.dbappsecurity.jsbridge.entity.jsBaseItem;
import com.dbappsecurity.jsbridge.entity.jsThirdTitle;
import com.dbappsecurity.jsbridge.webview.BWebView;
import com.dbappsecurity.jsbridge.webview.CompletionHandler;
import com.tbruyelle.rxpermissions2.RxPermissions;
import java.util.List;
import java.util.Objects;
import org.json.JSONObject;
import org.parceler.Parcels;

import static com.dbapp.uilib.widget.titlebar.TitleBar.OptionItem.TYPE_IMG_NET;
import static com.dbapp.uilib.widget.titlebar.TitleBar.POS_LEFT;

public class ThirdFragment extends InjectBaseFragment {

  @BindView(R.id.title_bar) TitleBar mTitleBar;

  @Override
  protected int getLayout() {
    return R.layout.fragment_main;
  }

  Bundle arguments = null;
  jsThirdTitle mThirdTitle = null;

  JsBaseFragment mJsBaseFragment = new JsBaseFragment() {

    @Override
    protected Activity getActivity() {
      return ThirdFragment.this.getActivity();
    }

    @Override
    protected RxPermissions getRxPermissions() {
      return ThirdFragment.this.permissions.get();
    }

    @Override
    protected String loadUrl() {
      return mThirdTitle.getUrl();
    }

    @Override
    public void initViewView() {
      mThirdTitle = Parcels.unwrap(
          Objects.requireNonNull(getArguments()).getParcelable("jsThirdTitle"));
      if (mThirdTitle==null) return;
      mTitleBar.setTitle(mThirdTitle.getTitle());
      mTitleBar.setBackgroundColor(mThirdTitle.getNavBackColor());
      TitleBar.OptionItem
          optionItem = mTitleBar.addItem(POS_LEFT,TYPE_IMG_NET,mThirdTitle.getBackIcon(),false);
      optionItem.setOnClickListener(v -> getActivity().finish());
    }
  };

  public static ThirdFragment getInstance(Bundle bundle) {
    ThirdFragment fragment = new ThirdFragment();
    fragment.setArguments(bundle);
    return fragment;
  }

  @Override
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mJsBaseFragment.onViewCreated(view);
  }

  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    return mJsBaseFragment.onKeyDown(keyCode, event)|super.onKeyDown(keyCode, event);
  }

  @Override
  public void onResume() {
    super.onResume();
    mJsBaseFragment.onResume();
  }

  @Override
  public void onPause() {
    super.onPause();
    mJsBaseFragment.onPause();
  }

  @Override
  public void onDestroyView() {
    super.onDestroyView();
    mJsBaseFragment.onDestroyView();
  }
}
